package org.compression.util;

import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.util.Iterator;

import javax.imageio.IIOImage;
import javax.imageio.ImageIO;
import javax.imageio.ImageWriter;
import javax.imageio.stream.ImageOutputStream;

import org.apache.pdfbox.pdfparser.PDFParser;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.rendering.ImageType;
import org.apache.pdfbox.rendering.PDFRenderer;
import org.apache.pdfbox.text.PDFTextStripper;

public class PdfUtil {



	/**
	 * 通过PDFbox获取文章总页数
	 *
	 * @param filePath:文件路径
	 * @return
	 * @throws IOException
	 */
	public static int getNumberOfPages(String filePath) throws IOException, InterruptedException {
		File file = new File(filePath);
		PDDocument pdDocument = PDDocument.load(new File(filePath));
		int pages = pdDocument.getNumberOfPages();
		pdDocument.close();
		return pages;
	}


	/**
	 * 通过PDFbox获取文章内容
	 *
	 * @param filePath
	 * @return
	 */
	public static String getContent(String filePath) throws IOException {
		PDFParser pdfParser = new PDFParser(new org.apache.pdfbox.io.RandomAccessFile(new File(filePath), "rw"));
		pdfParser.parse();
		PDDocument pdDocument = pdfParser.getPDDocument();
		String text = new PDFTextStripper().getText(pdDocument);
		System.out.println(text);
		pdDocument.close();

		return text;
	}

	/**
	 * 通过PDFbox生成文件的缩略图
	 *
	 * @param filePath：文件路径
	 * @param outPath：输出图片路径
	 * @throws IOException
	 */
	public static void getThumbnails(String filePath, String outPath) throws IOException {
		// 利用PdfBox生成图像
		PDDocument pdDocument = PDDocument.load(new File(filePath));
		PDFRenderer renderer = new PDFRenderer(pdDocument);

		// 构造图片
		BufferedImage img_temp = renderer.renderImageWithDPI(0, 30, ImageType.RGB);
		// 设置图片格式
		Iterator<ImageWriter> it = ImageIO.getImageWritersBySuffix("png");
		// 将文件写出
		ImageWriter writer = (ImageWriter) it.next();
		ImageOutputStream imageout = ImageIO.createImageOutputStream(new FileOutputStream(outPath));
		writer.setOutput(imageout);
		writer.write(new IIOImage(img_temp, null, null));
		img_temp.flush();
		imageout.flush();
		imageout.close();
		//Warning: You did not close a PDF Document
		pdDocument.close();
	}
	
	
	public static String readFdf(File pdfFile,int startPage,int endPage) throws Exception {
		//Document doc = LucenePDFDocument.getDocument( file );

		// 是否排序
		boolean sort = false;
		// 输入文本文件名称
		String textFileName = null;
		// 编码方式
			String encoding = "UTF-8";
	

		// 内存中存储的PDF Document
		PDDocument document = null;
		try {
			// 从本地装载文件
			//注意参数已不是以前版本中的URL.而是File。
			System.out.println("开始装载文件"+pdfFile.getName());
			document = PDDocument.load(pdfFile);
			// PDFTextStripper来提取文本
			PDFTextStripper stripper = null;
			stripper = new PDFTextStripper();
			// 设置是否排序
			stripper.setSortByPosition(sort);
			// 设置起始页
			stripper.setStartPage(startPage);
			// 设置结束页
			stripper.setEndPage(endPage);
			
			String text = stripper.getText(document);
			
			return text;
		}catch (Exception e) {
			e.printStackTrace();
		}
		return "0000";
	}

	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	

}
